# -*- coding:utf8 -*-
'''
日期： 2013-3-22
@作者:  Monk
'''
from google.appengine.api import urlfetch
import json,urllib,urllib2
class MonkWeibo():
    ##Authorization:OAuth  
    __apiurl__ = 'https://api.weibo.com/2/'
    def __init__(self,appKey,appSecret,token = False):
        self.appKey = appKey
        self.appSecret = appSecret
        if token:
            self.header = {}
            self.header['self.header'] = "OAuth  %s" % (token)
    #获取登录的链接
    def getUrl(self,redirect_uri):
        return "https://api.weibo.com/oauth2/authorize?client_id=%s&response_type=code&redirect_uri=%s" %(self.appKey,redirect_uri)
    def access_token(self):
        pass
    #GET数据
    def get(self,api,query = {}):
        url = self.__apiurl__ + api + ".json"
        return self.http(url, "GET", {})
    #Post数据
    def post(self,api,data = {}):
        url = self.__apiurl__ + api + ".json"
        return self.http(url, "POST", data)
    #处理请求
    def http(self,url,method,data = {}):
        try:
            rs = urlfetch.Fetch(url = url, payload = data, method= method, headers = self.header)
            return json.loads(rs.content)
        except Exception,e:
            return False